package com.clouds.wms.sal;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Salme;
import com.clouds.wms.bean.Salmf;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.SalmeService;
import com.clouds.wms.service.SalmfService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/salmf")
public class SalmfController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="salmeService")
	private SalmeService salmeService;
	@Resource(name="salmfService")
	private SalmfService salmfService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@RequestMapping(value = "/salmfMain.xhtml")
	public String salmfMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String meaa = RequestHelper.getString(request, "meaa");
		String meab = RequestHelper.getString(request, "meab");
		Salme salme = salmeService.getSalme(meaa, meab);
		model.addAttribute("meaa", meaa);
		model.addAttribute("meab", meab);
		model.addAttribute("salme", salme);
//		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGDD));
		return "sal/salmf";
	}
	@RequestMapping(value = "/salmfList.xhtml")
	public String salmfList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String meaa = RequestHelper.getString(request, "meaa");
		String meab = RequestHelper.getString(request, "meab");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = salmfService.getSalmfList(meaa, meab, pageNumber, pageSize);
		List<Salmf> list = (List<Salmf>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addSalmf.xhtml", method = RequestMethod.POST)
	public String addSalme(HttpServletRequest request, HttpServletResponse response, Model model, Salmf salmf) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmf.setMfac(salmfService.getMaxMfac());
//		salmf.setMfam(0);
		salmfService.saveSalcb(salmf);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + salmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 更新采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateSalmf.xhtml", method = RequestMethod.POST)
	public String updateSalme(HttpServletRequest request, HttpServletResponse response, Model model, Salmf salmf) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmfService.updateSalcb(salmf);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("更新了编号为[" + salmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delSalmf.xhtml")
	public String delSalmf(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "mfaa");
		String mfab = RequestHelper.getString(request, "mfab");
		String mfac = RequestHelper.getString(request, "mfac");
		StringBuffer mfacs = new StringBuffer();
		for(String s : mfac.split(",")){
			mfacs.append("'").append(s).append("',");
		}
		mfacs = mfacs.delete(mfacs.length()-1, mfacs.length());
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmfService.delSalmf(mfaa, mfab, mfacs.toString());
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mfaa+","+mfab+","+mfac + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delSalmfAll.xhtml")
	public String delSalmfAll(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "mfaa");
		String mfab = RequestHelper.getString(request, "mfab");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmfService.delSalmfAll(mfaa, mfab);
		out.print("success");
		return null;
	}
	/**
	 * 变更采购单状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/auditSalmf.xhtml", method = RequestMethod.POST)
	public String auditSalmf(HttpServletRequest request, HttpServletResponse response, Model model, Salmf salmf) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "meaa");
		String mfab = RequestHelper.getString(request, "meab");
		String status = RequestHelper.getString(request, "flag");
//		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmfService.changeStatus(mfaa, mfab, status);
		
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delSalmemf.xhtml", method = RequestMethod.POST)
	public String delSalmemf(HttpServletRequest request, HttpServletResponse response, Model model, Salmf salmf) throws SysException, IOException {
		String mfaa = RequestHelper.getString(request, "meaa");
		String mfab = RequestHelper.getString(request, "meab");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		salmfService.delSalmemf(mfaa, mfab);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + salmf.getMfab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
}
